<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Groups/classes/PhpFox_ComponentInviteToGroup.class.php, $Author:  $ */



/** Component for display invite to group dialog
 * @version $Id: PhpFox_ComponentInviteToGroup.class.php
 *  Max Kovrigovich
 * @package module.groups
 */
class PhpFox_Mod_Groups_ComponentInviteToGroup extends BaseComponent
{
    /** Processing method
     */

    function process()
    {
    	App::membersOnly();

        //check permissions
        $oSecurityService = App::getModuleService('Account', 'Security');
		/* @var $oSecurityService PhpFox_Mod_Account_ServiceSecurity */
		$oSecurityService->checkPerm('groups', array('read_only', 'full'));
        $oMessengerService = &App::getModuleService('Messenger', 'Messenger');
        $oGroupsService = $this->_oModule->getService('Groups');
        /* @var $oGroupsService PhpFox_Mod_Groups_ServiceGroups */

        $oTpl = $this->_oModule->getTemplate();

        $oReq = &App::getRequest();
        /* @var $oReq Request */

        $oUser = $this->getParam('user');
        $sUser = $oReq->get('invuser', '');

        $iGroupId = $oReq->getInt('group', 0);
        $bInvited = 0;
        $bErrors = 0;

        $bNoUser = 0;
        $bSelf = 0;
        $bIsMember = 0;
        $bNoGroups = 0;

        //load owners groups
	    // load groups where user is member or creator
        $aGroups = $oGroupsService->getMemberGroups($oUser->aData['user']);
        foreach($aGroups as $k=>$v)
        {
            if($oGroupsService->isMember($v['id'], $sUser) ||
               $oGroupsService->getInviteData($v['id'], $sUser))
                unset($aGroups[$k]);
        }

        $bNoGroups = (count($aGroups) == 0);
        $oInvUser = $oSecurityService->getUserByName($sUser);
        if(!$oInvUser)
            $bNoUser = true;

        if($iGroupId && !$bNoUser)
        {
            if($oGroupsService->isMember($iGroupId, $sUser))
            {
                $bIsMember = true;
            }
            elseif($oGroupsService->isOwner($iGroupId, $sUser))
            {
                $bSelf = true;
            }
            else
            {
                $bInvited = true;
                $aGroupData = $oGroupsService->getGroupData($iGroupId);
                $oGroupsService->addInvite($oUser->aData['user'], $sUser, $iGroupId, $aGroupData['type']);

				//send notification
                $sInviteText=App::format('Mod_Groups.mail_user_invited_to_group', array(App::makeUrl('public.groups.view', array('id' => $iGroupId)), $aGroupData['title']));
                $oMessengerService->sendMessage(array('to'=>$sUser, 'title'=>App::format('Mod_Groups.title.invite'),'text'=>$sInviteText), $oUser->aData['user'], 'mail');
                 $oTpl->assign('nGroupId', $iGroupId);
            }
        }
        $bErrors = ($bNoUser || $bSelf || $bIsMember || $bNoGroups);

        $oTpl->assign(array(
            'aGroups' => $aGroups,
            'sUser'   => $sUser,
	        'bInvited' => $bInvited,
	        'bErrors' => $bErrors,
	        'bNoUser' => $bNoUser,
	        'bSelf' => $bSelf,
	        'bIsMember' => $bIsMember,
	        'bNoGroups' => $bNoGroups,
        ));
        $sHtml = $oTpl->fetch('InviteToGroup.html');
        return $sHtml;
    }
}
?>